package com.workstudy.controller;

import com.workstudy.domain.Course;
import com.workstudy.service.CourseService;
import com.workstudy.support.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


@RequestMapping("course")
@Controller
public class CourseController extends BaseController {

    @Autowired
    private CourseService courseService;


    /**
     * 课程表详情
     * @param userId
     * @param model
     * @return
     */
    @RequestMapping("detail/{userId}")
    public String detail(@PathVariable Integer userId, Model model) {
        Course course = new Course();
        course.setUserId(userId);
        course = courseService.selectOne(course);
        if (null != course) model.addAttribute(course);
        else model.addAttribute(new Course());
        model.addAttribute("userId",userId);
        return "course/detail";
    }

    /**
     * 更新课程表
     * @param course
     * @return
     */
    @RequestMapping("save")
    public String save(Course course){
        if(course.getId() == 0 ){
            course.setId(null);
            courseService.insertSelective(course);
        }else {
            courseService.updateByIdSelective(course);
        }
        return "redirect:/user/list";
    }

}
